package com.kopo.droidlipokit;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.kopo.droidlipokit.R;
import com.kopo.droidioiolib.Behavior;
import com.kopo.droidlipokit.dummy.DummyContent;

/**
 * A fragment representing a single Item detail screen.
 * This fragment is either contained in a {@link ItemListActivity}
 * in two-pane mode (on tablets) or a {@link ItemDetailActivity}
 * on handsets.
 */
public class ItemDetailFragment extends Fragment {
	public ItemDetailFragment me;
    /**
     * The fragment argument representing the item ID that this fragment
     * represents.
     */
    public static final String ARG_ITEM_ID = "item_id";

    /**
     * The dummy content this fragment is presenting.
     */
    private DummyContent.DummyItem mItem;

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public ItemDetailFragment() {
    }

    // This is used to populate the content of the selected item
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments().containsKey(ARG_ITEM_ID)) {
            // Load the dummy content specified by the fragment
            // arguments. In a real-world scenario, use a Loader
            // to load content from a content provider.
            mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
        }
        me = this;
    }

    // This is used to setup the selected detail view. 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

    	View rootView = null;
        if (mItem != null) {
	    	
	    	int iid = Integer.valueOf(mItem.id).intValue();

	    	System.out.println("----ItemID: " + iid);

	    	switch( iid )
	    	{
		    	case 2: // test View
			        rootView = setupTestView(inflater, container, savedInstanceState);
		    		break;
		    	case 3: // Sensor View
			        rootView = setupSensorView(inflater, container, savedInstanceState);
		    		break;
		    	case 4: // Help
			        rootView = setupHelpView(inflater, container, savedInstanceState);
		    		break;
		    	case 1: // Behavior
	    	        rootView = setupBehaviorView(inflater, container, savedInstanceState);
	    			break;
    			default:
    				break;
	    	}
        }

        if( rootView == null )
        {
	        rootView = inflater.inflate(R.layout.fragment_item_detail, container, false);
	        ((TextView) rootView.findViewById(R.id.item_detail)).setText(mItem.content);
	    }

        return rootView;
    }

    private View setupHelpView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
    	String msg = "DeroidDrive v0.1\n\nDownload software from: http://www.kopo.com\nJoin Robotics Interest Group\n\n";
    	msg = msg + "Using this software.\n";
    	msg = msg + "1. Power up the assembled robot kit device.\n";
    	msg = msg + "2. Bluetooth pair (or USB connect) with the device if not done.\n";
    	msg = msg + "3. Select a behavior from the main menu.\n";
    	msg = msg + "4. Sensor view is to test sensors.\n";
    	msg = msg + "5. Test view is to test PWM and servo motors.\n";
    	msg = msg + "6. Behavior view is to run preprogrammed autonomous behaviors.\n\n";
    	
    	msg = msg + "Step by step instruction of building the robot kit.\n";
    	msg = msg + "1.	Power IOIO board:\n";
    	msg = msg + "Connect the the battery of the RC car to VIN and GND pin.\n";
    	msg = msg + "2.	Connect IOIO to Android Phone using Bluetooth (optional). You can directly connect Android phone with IOIO using the provided USB cable.\n";
    	msg = msg + "Insert the Bluetooth dongle to IOIO. Pair with Android Phone: instruction is here.\n";
    	msg = msg + "https://github.com/ytai/ioio/wiki/IOIO-Over-Bluetooth\n";
    	msg = msg + "3.	Connect sensors:\n";
    	msg = msg + "Sensor1: IOIO Pin 41 to Vo of the Sharp IR sensor. Vcc (red) to +5V of IOIO.  GND to GND\n";
    	msg = msg + "Sensor2: IOIO Pin 42 to Vo of the Sharp IR sensor. Vcc (red) to +5V of IOIO.  GND to GND\n";
    	msg = msg + "4.	Connect servo motors:\n";
    	msg = msg + "Servo 1: Red cable to IOIO +5V, Brown to IOIO GND. Orange to IOIO Pin 6. Connect 10K Ohm to IOIO Pin 6 and +5V\n";
    	msg = msg + "5.	Connect Ardumoto - Motor Driver Shield to IOIO\n";
    	msg = msg + "IOIO Pin 10  to Ardumoto PWMA\n";
    	msg = msg + "IOIO Pin 11  to Ardumoto DIRA\n";
    	msg = msg + "IOIO Pin 12  to Ardumoto PWMB\n";
    	msg = msg + "IOIO Pin 13  to Ardumoto DIRB\n";
    	msg = msg + "6.	Connect the rear wheel motor of the RC car to Ardumoto\n";

    	View rootView = inflater.inflate(R.layout.fragment_item_detail, container, false);
    	((TextView) rootView.findViewById(R.id.item_detail)).setText(msg);
    	return rootView;
    }

    private View setupBehaviorView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
     	Log.d("kopo.log", "ItemDetailFragment.setupBehaviorView");
    	View rootView;
    	rootView = inflater.inflate(R.layout.frag_detail_remote, container, false);

    	// Create a behavior which will control the device and the view
    	Behavior bh = new BehaviorGeneral(rootView);
    	bh.setup();
    	MyControlDevice.currentBehavior = bh;
    	bh.setEnabled(true);

       	MyControlDevice.me.start();

    	return rootView;
    }

    private View setupTestView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
    	Log.d("kopo.log", "ItemDetailFragment.setupRemoteControlView");
    	View rootView;
    	rootView = inflater.inflate(R.layout.frag_detail_test, container, false);
    	// ((TextView) rootView.findViewById(R.id.item_detail)).setText(mItem.content);

     	// TODO attach event handlers

    	// Create a behavior which will control the device and the view
    	Behavior bh = new BehaviorTest(rootView);
    	bh.setup();
    	MyControlDevice.currentBehavior = bh;
    	bh.setEnabled(true);

       	MyControlDevice.me.start();

    	return rootView;
    }

    //private ImageView imageView;    
	private View setupSensorView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
		Log.d("kopo.log", "ItemDetailFragment.setupSensorView");

    	View rootView;
    	System.out.println(com.kopo.droidlipokit.BehaviorSensor.roundedTemp);
    	//rootView = inflater.inflate(R.layout.temp_display_normal, container, false);
    	
    	/*Switch layout according to the temperature value recorded
    	 *not succeed yet 
    	 */
    	if (BehaviorSensor.roundedTemp >30.0){
    		rootView = inflater.inflate(R.layout.temp_display_high, container, false);		
    	
    		
    	}else if (BehaviorSensor.roundedTemp<26.0){
    		rootView = inflater.inflate(R.layout.temp_display_low, container, false);
    	
    	}else {
    		rootView = inflater.inflate(R.layout.temp_display_normal, container, false);
    	}
    	 
    	 
    	Behavior bh = new BehaviorSensor(rootView);
    	MyControlDevice.currentBehavior = bh;
    	bh.setEnabled(true);
    	
    	MyControlDevice.me.start();

    	return rootView;

    	// Create a behavior which will control the device and the view
    	

    	// No event handlers

    	// ControlInterface loop will update sensor readings to temp_display_normal
    	

    }
    
    public void onDestroyView()
    {
    	Log.d("kopo.log", "ItemDetailFragment.onDestroyView");
    	if( MyControlDevice.me != null )
    	{
    		MyControlDevice.me.stop();
    	}
    	super.onDestroyView();
    }
    
    
}

